package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：libowen
 * @date ：Created in 2020/1/2
 * @description ：菜单service实习类
 * @version: 1.0
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;


    @Override
    @Transactional
    public List<Menu> selectByUserId(Integer userId) {
        log.info("用户Id:{}", userId);
        //根据id调用dao获取主菜单


        List<Menu> menuList = menuDao.selectByUserId(userId);


        List<Menu> parentMenuList = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getParentMenuId() == null){
                parentMenuList.add(menu);
            }
        }

        for (Menu pm : parentMenuList) {
            List<Menu> menus = putChildren(pm, menuList);
            pm.setChildren(menus);

        }
        return parentMenuList;
    }


    public List<Menu> putChildren(Menu parent, List<Menu> menuList){

        List<Menu> childList = new ArrayList<>();
        for (Menu m : menuList) {
            if (parent.getId().equals(m.getParentMenuId())){
                List<Menu> menus = putChildren(m, menuList);
                m.setChildren(menus);
                childList.add(m);
            }
        }

        return childList;

    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //调用dao查询
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void edit(Menu menu) {
        log.info("更新菜单data:{}",menu);
        //调用dao更新
        menuDao.update(menu);
    }

    @Override
    public void add(Menu menu) {
        log.info("添加菜单项:{}",menu);
        if(null == menu.getParentMenuId()){
            //调用dao添加
            String parentPath = menuDao.selectPath();
            parentPath= String.valueOf(Integer.parseInt(parentPath)+1);
            menu.setPath(parentPath);
            menuDao.insert(menu);
            return;
        }

        //根据获取父Id获取path最大的字段
        String path = menuDao.pathByParentMenuId(menu.getParentMenuId());
        if(null != path){

            String[] split = path.split("-");
            //判断副菜单是否到达上限
            if(Integer.parseInt(split[1]) == 9 ){
                throw new BusinessRuntimeException("当前副菜单已满");
            }
            int endPath = Integer.parseInt(split[1])+1;
            path = split[0] +"-"+ endPath;

            menu.setPath(path);
          }else{
           path = menuDao.selectParentMenuPath(menu.getParentMenuId());
           path = "/"+path +"1";
            menu.setPath(path);
        }
        menuDao.insert(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.selectById(id);
    }

    @Override
    public void delById(Integer id) {
        //查询是否有关联信息
        Long count = menuDao.countRoleByMenuId(id);
        if(count >0){
            throw new BusinessRuntimeException("当前菜单有关联数据，不能删除");
        }
        menuDao.delById(id);
    }

    @Override
    public List<Menu> findParentMenu() {
        return menuDao.selectParentMenu();
    }

}
